/**
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 */
package com.jeesite.modules.settlement.web.customercarcontract;

import com.alibaba.fastjson.JSONValidator;
import com.jeesite.common.codec.EncodeUtils;
import com.jeesite.common.collect.ListUtils;
import com.jeesite.common.config.Global;
import com.jeesite.common.entity.Page;
import com.jeesite.common.lang.StringUtils;
import com.jeesite.common.web.BaseController;
import com.jeesite.modules.base.entity.cars.Tcar;
import com.jeesite.modules.base.entity.customer.Customer;
import com.jeesite.modules.base.service.cars.TcarService;
import com.jeesite.modules.base.service.customer.CustomerService;
import com.jeesite.modules.order.entity.serviceorder.ServiceOrder;
import com.jeesite.modules.order.service.serviceorder.ServiceOrderService;
import com.jeesite.modules.settlement.entity.customercarcontract.CustomerCarContract;
import com.jeesite.modules.settlement.entity.customercarcontract.CustomerCarContractDetail;
import com.jeesite.modules.settlement.entity.customersettlement.CustomerSettlement;
import com.jeesite.modules.settlement.entity.customersettlement.CustomerSettlementDetail;
import com.jeesite.modules.settlement.service.customercarcontract.CustomerCarContractService;
import com.jeesite.modules.settlement.service.customersettlement.CustomerSettlementService;
import com.jeesite.modules.sys.entity.Company;
import com.jeesite.modules.sys.entity.EmpUser;
import com.jeesite.modules.sys.service.EmpUserService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 客户汽车供需合同Controller
 *
 * @author LV
 * @version 2021-03-06
 */
@Controller
@RequestMapping(value = "${adminPath}/settlement/customerCarContract")
public class CustomerCarContractController extends BaseController {

    @Autowired
    private CustomerCarContractService customerCarContractService;
    @Autowired
    private ServiceOrderService serviceOrderService;
    @Autowired
    private CustomerService customerService;
    @Autowired
    private EmpUserService empUserService;
    @Autowired
    private CustomerSettlementService customerSettlementService;
    @Autowired
    private TcarService tcarService;

    /**
     * 获取数据
     */
    @ModelAttribute
    public CustomerCarContract get(String customerCarContractId, boolean isNewRecord) {
        return customerCarContractService.get(customerCarContractId, isNewRecord);
    }

    /**
     * 查询列表
     */
    @RequiresPermissions("settlement:customerCarContract:view")
    @RequestMapping(value = {"list", ""})
    public String list(CustomerCarContract customerCarContract, Model model) {
        model.addAttribute("listServiceOrder", serviceOrderService.findList(new ServiceOrder()));// 服务订单号
        model.addAttribute("listCustomer", customerService.findList(new Customer()));
        model.addAttribute("listUser", empUserService.findList(new EmpUser()));
        model.addAttribute("customerCarContract", customerCarContract);
        return "modules/settlement/customercarcontract/customerCarContractList";
    }

    /**
     * 查询列表数据
     */
    @RequiresPermissions("settlement:customerCarContract:view")
    @RequestMapping(value = "listData")
    @ResponseBody
    public Page<CustomerCarContract> listData(CustomerCarContract customerCarContract, HttpServletRequest request, HttpServletResponse response) {
        customerCarContract.setPage(new Page<>(request, response));
        Page<CustomerCarContract> page = customerCarContractService.findPage(customerCarContract);
        return page;
    }

    /**
     * 查看编辑表单
     */
    @RequiresPermissions("settlement:customerCarContract:view")
    @RequestMapping(value = "form")
    public String form(CustomerCarContract customerCarContract, Model model) {
        model.addAttribute("listServiceOrder", serviceOrderService.findList(new ServiceOrder()));// 服务订单号
        model.addAttribute("listCustomerSettlement", customerSettlementService.findList(new CustomerSettlement()));// 客户结算单
        model.addAttribute("listCustomer", customerService.findList(new Customer()));
        for (CustomerCarContractDetail customerCarContractDetail : customerCarContract.getCustomerCarContractDetailList()) {
            customerCarContractDetail.setCarName(customerCarContractDetail.getTcar().getCarName());
        }
        model.addAttribute("customerCarContract", customerCarContract);
        return "modules/settlement/customercarcontract/customerCarContractForm";
    }

    /**
     * 查看表单详情
     */
    @RequiresPermissions("settlement:customerCarContract:view")
    @RequestMapping(value = "detail")
    public String detail(CustomerCarContract customerCarContract, Model model) {
        model.addAttribute("listServiceOrder", serviceOrderService.findList(new ServiceOrder()));// 服务订单号
        model.addAttribute("listCustomerSettlement", customerSettlementService.findList(new CustomerSettlement()));// 客户结算单
        model.addAttribute("listCustomer", customerService.findList(new Customer()));
        model.addAttribute("customerCarContract", customerCarContract);
        model.addAttribute("tcarList", tcarService.findList(new Tcar()));
        return "modules/settlement/customercarcontract/customerCarContractDetail";
    }

    /**
     * 保存数据
     */
    @RequiresPermissions("settlement:customerCarContract:edit")
    @PostMapping(value = "save")
    @ResponseBody
    public String save(@Validated CustomerCarContract customerCarContract) {
        customerCarContractService.save(customerCarContract);
        return renderResult(Global.TRUE, text("保存客户汽车供需合同成功！"));
    }

    /**
     * 删除数据
     */
    @RequiresPermissions("settlement:customerCarContract:delete")
    @RequestMapping(value = "delete")
    @ResponseBody
    public String delete(CustomerCarContract customerCarContract) {
        customerCarContractService.delete(customerCarContract);
        return renderResult(Global.TRUE, text("删除客户汽车供需合同成功！"));
    }

    /**
     * 列表选择对话框
     */
    @RequiresPermissions("settlement:customerCarContract:view")
    @RequestMapping(value = "customerCarContractSelect")
    public String empUserSelect(CustomerCarContract customerCarContract, String selectData, Model model) {
        String selectDataJson = EncodeUtils.decodeUrl(selectData);
        if (selectDataJson != null && JSONValidator.from(selectDataJson).validate()) {
            model.addAttribute("selectData", selectDataJson);
        }
        model.addAttribute("CustomerCarContract", customerCarContract);
        return "modules/settlement/customercarcontract/customerCarContractSelect";
    }

    /**
     * 创建自 客户结算单
     */
    @RequiresPermissions("settlement:customerCarContract:view")
    @RequestMapping(value = "createFromSettle")
    public String createFromSettle(CustomerSettlement customerSettlement, Model model) {
        CustomerSettlement oldCustomerSettlement = customerSettlementService.get(customerSettlement.getCustomerSettlementId());
        CustomerCarContract customerCarContract = new CustomerCarContract();
        if (oldCustomerSettlement != null) {
            // 客户结算单
            customerCarContract.setCustomerSettlementId(customerSettlement.getCustomerSettlementId());
            // 服务订单
            customerCarContract.setServiceOrderId(customerSettlement.getServiceOrderId());
            // 客户
            customerCarContract.setCustomerId(oldCustomerSettlement.getCustomerId());
            // 开票客户
            if (StringUtils.isNotEmpty(oldCustomerSettlement.getInvoiceCustomerId())) {
                customerCarContract.setInvoiceCustomerId(oldCustomerSettlement.getInvoiceCustomerId());
            } else {
                customerCarContract.setInvoiceCustomerId(oldCustomerSettlement.getCustomerId());
            }
            // 代理公司
            Company company = new Company();
            company.setCompanyCode(serviceOrderService.get(customerSettlement.getServiceOrderId()).getCompany().getCompanyCode());
            customerCarContract.setCompany(company);
            // 结算单列表
            List<CustomerSettlementDetail> customerSettlementDetailList = oldCustomerSettlement.getCustomerSettlementDetailList();
            List<CustomerCarContractDetail> customerCarContractDetailList = ListUtils.newArrayList();
            for (CustomerSettlementDetail customerSettlementDetail : customerSettlementDetailList) {
                CustomerCarContractDetail customerCarContractDetail = new CustomerCarContractDetail();
                customerCarContractDetail.setFrameNumber(customerSettlementDetail.getFrameNumber());
                customerCarContractDetail.setCarId(customerSettlementDetail.getCarId());
                customerCarContractDetail.setCarName(customerSettlementDetail.getTcar().getCarName());
                customerCarContractDetail.setCalculatePrice(customerSettlementDetail.getActualAmount());
                customerCarContractDetailList.add(customerCarContractDetail);
            }
            customerCarContract.setCustomerCarContractDetailList(customerCarContractDetailList);

            model.addAttribute("listServiceOrder", serviceOrderService.findList(new ServiceOrder()));// 服务订单号
            model.addAttribute("listCustomer", customerService.findList(new Customer()));
            model.addAttribute("listCustomerSettlement", customerSettlementService.findList(new CustomerSettlement()));// 客户结算单
            model.addAttribute("customerCarContract", customerCarContract);
            return "modules/settlement/customercarcontract/customerCarContractForm";
        } else {
            return renderResult(Global.FALSE, text("未查找到服务订单的相关信息！"));
        }
    }

}